package com.troy.main;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.lang.reflect.Method;

/**
 * Created by llx on 16/9/1.
 */
public class Main {

    public static void main(String[] args){
        String path = Main.class.getResource("/test.json").getPath();
        JSONObject json = new JSONObject(readStrFile(path));
        puts(json.toString());
        JSONArray keys = json.getJSONArray("keys");
        JSONArray data = json.getJSONArray("data");
        JSONObject result = new JSONObject();
        for(int j = 0; j < data.length(); ++j){
            JSONObject attr = data.getJSONObject(j);
            String attr_key = attr.getString("attr_key");
            String value = attr.getString("value");
            String index = attr.getString("index");
            for(int i = 0; i < keys.length(); ++i){
                JSONObject key = keys.getJSONObject(i);
                String local = key.getString("local");
                String remote = key.getString("remote");
                String form = key.getString("form");
//              如果key都不相等，那后边就没必要处理了
                if(!attr_key.equals(remote)){
                    continue;
                }

                JSONObject formOs;
                if(result.has(form)){
                    formOs = result.getJSONObject(form);
                }else {
                    formOs = new JSONObject();
                    result.put(form, formOs);
                }
                Object formO;
                if(formOs.has(index)){
                    formO = formOs.get(index);
                }else{
                    formO = newObjByName(form);
                    formOs.put(index, formO);
                }
                if(formO == null){
                    puts("can not find class with name:" + form);
                    continue;
                }
                Class clazz = null;
                try {
                    clazz = Class.forName("com.troy.models." + form);
                    Method[] methods = clazz.getMethods();
                    for(Method m : methods){
                        if(m.getName().equals(local)){
                            m.invoke(formO, value);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        puts("result:" + result.toString());
    }

    public static Object newObjByName(String name){
        Object tmp = null;
        try {
            Class clazz = Class.forName("com.troy.models." + name);
            tmp = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tmp;
    }

    public static String readStrFile(String filePath){
        puts(filePath);
        File f = new File(filePath);
        String result = "";
        if(f.isFile() && f.exists()){ //判断文件是否存在
            InputStreamReader read = null;//考虑到编码格式
            try {
                read = new InputStreamReader(
                        new FileInputStream(f), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    result = result + lineTxt;
                }
                read.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            puts("file not exist");
        }
        puts(result);
        return result;
    }

    public static void puts(String str){
        System.out.println(str);
    }

}
